package com.yjf.question.home;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjf.question.utils.ResultUtils;
import com.yjf.question.utils.ResultVo;
import com.yjf.question.wxapi.question.entity.AnswerQuestion;
import com.yjf.question.wxapi.question.entity.SysQuestion;
import com.yjf.question.wxapi.question.entity.SysQuestionParn;
import com.yjf.question.wxapi.question.service.AnswerQuestionService;
import com.yjf.question.wxapi.question.service.SysQuestionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("wxapi/home")
public class HomeController {

    @Resource
    private SysQuestionService sysQuestionService;

    @Resource
    private AnswerQuestionService answerQuestionService;


    @GetMapping("/getList")
    public ResultVo getList(SysQuestionParn sysQuestionParn){
//        创建Page对象
        IPage<SysQuestion> page = new Page<>();
        page.setCurrent(sysQuestionParn.getCurrentPage());
        page.setSize(sysQuestionParn.getPageSize());
        IPage<SysQuestion> list = sysQuestionService.getList(page);
        return ResultUtils.success("查询成功",list);
    }


    @GetMapping("/getDetails")
    public ResultVo getDetails(String questionId,String openid){

//        1.根据questionId查询问卷
        SysQuestion question = sysQuestionService.getById(questionId);

//        2.根据questionId和openId查询问卷
        QueryWrapper<AnswerQuestion> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<AnswerQuestion> lambdaQueryWrapper = queryWrapper.lambda();
        lambdaQueryWrapper.eq(AnswerQuestion::getQuestionId,questionId)
                .eq(AnswerQuestion::getOpenid,openid);
        AnswerQuestion answerQuestion = answerQuestionService.getOne(lambdaQueryWrapper);

//        判断answerQuestion是否为空
        if (answerQuestion!=null){
//            表示这个问卷已经回答了
            question.setStatus("1");
        }else {
            question.setStatus("0");
        }


        return ResultUtils.success("查询成功",question);

    }

}
